package com.zzj.ecology.framework.aliyun.oss.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import com.zzj.ecology.framework.aliyun.oss.SysOssService;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStreamReader;

@Service("sysOssService")
public class SysOssServiceImpl implements SysOssService {

        String endpoint = "http://oss-cn-shanghai.aliyuncs.com";
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
        String accessKeyId = "LTAI4G7FiUuadDLKFQzQgUyR";
        String accessKeySecret = "NdeLA5kbfjemqdcbVedrtNPCVj1EG6";
        String bucketName = "fapd2016";



    @Override
    public void listObject() {
        // Endpoint以杭州为例，其它Region请按实际情况填写。

//        // 创建OSSClient实例。
//        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//
//        // ossClient.listObjects返回ObjectListing实例，包含此次listObject请求的返回结果。
//        ObjectListing objectListing = ossClient.listObjects(bucketName);
//        // objectListing.getObjectSummaries获取所有文件的描述信息。
//        for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
//            System.out.println(" - " + objectSummary.getKey() + "  " +
//                    "(size = " + objectSummary.getSize() + ")");
//        }
//
//        // 关闭OSSClient。
//        ossClient.shutdown();

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// 列举所有的Object的版本信息。
        String nextKeyMarker = null;
        String nextVersionMarker = null;
        VersionListing versionListing = null;
        do {
            ListVersionsRequest listVersionsRequest = new ListVersionsRequest()
                    .withBucketName(bucketName)
                    .withKeyMarker(nextKeyMarker)
                    .withVersionIdMarker(nextVersionMarker);

            versionListing = ossClient.listVersions(listVersionsRequest);
            for (OSSVersionSummary ossVersion : versionListing.getVersionSummaries()) {
                System.out.println("key name: " + ossVersion.getKey());
                System.out.println("versionid: " + ossVersion.getVersionId());
                System.out.println("Is latest: " + ossVersion.isLatest());
                System.out.println("Is delete marker: " + ossVersion.isDeleteMarker());
                System.out.println("size: " + ossVersion.getSize());
                System.out.println("BucketName: " + ossVersion.getBucketName());
                System.out.println("LastModified: " + ossVersion.getLastModified());
                System.out.println("ETag: " + ossVersion.getETag());
                System.out.println("StorageClass: " + ossVersion.getStorageClass());
                System.out.println("Own.getDisplayName: " + ossVersion.getOwner().getDisplayName());
                System.out.println();
                System.out.println();

                System.out.println("---------------------------------------------------------------");
            }

            nextKeyMarker = versionListing.getNextKeyMarker();
            nextVersionMarker = versionListing.getNextVersionIdMarker();
        } while (versionListing.isTruncated());

// 关闭OSSClient。
        ossClient.shutdown();




    }

    @Override
    public void dlByVersionId() throws Exception {

        String objectName = "version.xlsx";
        String versionid = "CAEQFhiBgIDBh5OgkBciIDhjMWI3YjcwNWJjMTRhM2JiZWJiZmIwODYwNzUwZGQw";

// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// 封装GetObject请求。
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectName);
// 指定object版本id。
        getObjectRequest.setVersionId(versionid);

        ossClient.getObject(getObjectRequest, new File("D://dlversion.xlsx"));

// ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
        OSSObject ossObject = ossClient.getObject(getObjectRequest);

// 查看下载文件的版本id。
        System.out.println("Get Object versionid:" +  ossObject.getObjectMetadata().getVersionId());
// 读取文件内容。
        System.out.println("Object content:");
        BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
        while (true) {
            String line = reader.readLine();
            if (line == null) break;
            System.out.println("\n" + line);
        }

// 关闭OSSClient。
        ossClient.shutdown();
    }

    @Override
    public void put() {

// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// 以上传字符串为例。
        String content = "Hello OSS";
        PutObjectResult result = ossClient.putObject(bucketName, "me.txt", new ByteArrayInputStream(content.getBytes()));
// 查看此次上传object的版本id。
        System.out.println("result.versionid: " + result.getVersionId());

// 关闭OSSClient。
        ossClient.shutdown();
    }


}
